#include <iostream>
#include <QApplication>
#include <QDesktopWidget>
#include"opencv/cv.h"
#include"opencv2/opencv.hpp"
#include"opencv/highgui.h"
#include "mainwindow.h"
#include "capturemanager.h"
#include "frame.h"
#include "recognitionview.h"
#include "programlauncher.h"
#include "saveimagedialog.h"
#include "dbimagemanager.h"
#include "cvmanager.h"

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"

#define ERROR -1
#define NO_ERROR 1

using namespace std;
using namespace views;
using namespace models;
using namespace controllers;
using namespace cv;

/// Global variables
Mat src, src_gray;

int maxCorners = 23;
int maxTrackbar = 100;

RNG rng(12345);
char* source_window = "Image";

/// Function header
void goodFeaturesToTrack_Demo( int, void* );

int main(int argc, char *argv[])
{
    ProgramLauncher pl;
    pl.startApplication(argc, argv);

    /*Mat img1 = imread( "F:/Adama/Session Hiver 2014/LOG792/Remises/code/Recyk/data/imgDB/tests/3.png");
    Mat img2 = imread( "F:/Adama/Session Hiver 2014/LOG792/Remises/code/Recyk/data/imgDB/1.png");

    Mat result = CVManager::findFeatures(img1, img2);

    //-- Show detected matches
    imshow( "Good Matches & Object detection", result );
    waitKey(0);*/

    /*
      /// Load source image and convert it to gray
      src = imread( "../Recyk/data/imgDB/5.png", 1 );
      cvtColor( src, src_gray, CV_BGR2GRAY );

      /// Create Window
      namedWindow( source_window, CV_WINDOW_AUTOSIZE );

      /// Create Trackbar to set the number of corners
      createTrackbar( "Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );

      imshow( source_window, src );

      goodFeaturesToTrack_Demo( 0, 0 );

      waitKey(0);
      return(0);*/

    /*cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    cv::VideoCapture cap(0);
    cv::BackgroundSubtractorMOG2 bg;
    //bg.nmixtures = 3;
    //bg.bShadowDetection = false;
    bg.set("nmixtures", 3);
    bg.set("detectShadows", false);
    std::vector<std::vector<cv::Point> > contours;
    cv::namedWindow("Frame");
    cv::namedWindow("Background");
    for(;;)
    {
        cap >> frame;
        bg.operator ()(frame,fore);
        bg.getBackgroundImage(back);
        cv::erode(fore,fore,cv::Mat());
        cv::dilate(fore,fore,cv::Mat());
        cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
        cv::imshow("Frame",frame);
        cv::imshow("Background",back);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;*/



    /*
    int camera_index = 0;
    IplImage *iplImage = NULL;
    QImage qImage;
    int exit_key_press = 0;

    CaptureManager cm(camera_index);
    cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE);

    while (exit_key_press != 'q')
    {
        // Capture a frame
        qImage = cm.captureQImage(r.getFrameSize());
        r.setFrame(qImage);

        exit_key_press = cvWaitKey(1);
    }
    cvDestroyWindow("Grayscale video");
    delete(&cm);*/

    return 0;
}

/**
 * @function goodFeaturesToTrack_Demo.cpp
 * @brief Apply Shi-Tomasi corner detector
 */
void goodFeaturesToTrack_Demo( int, void* )
{
  if( maxCorners < 1 ) { maxCorners = 1; }

  /// Parameters for Shi-Tomasi algorithm
  vector<Point2f> corners;
  double qualityLevel = 0.01;
  double minDistance = 10;
  int blockSize = 3;
  bool useHarrisDetector = false;
  double k = 0.04;

  /// Copy the source image
  Mat copy;
  copy = src.clone();

  /// Apply corner detection
  goodFeaturesToTrack( src_gray,
               corners,
               maxCorners,
               qualityLevel,
               minDistance,
               Mat(),
               blockSize,
               useHarrisDetector,
               k );


  /// Draw corners detected
  cout<<"** Number of corners detected: "<<corners.size()<<endl;
  int r = 4;
  for( int i = 0; i < corners.size(); i++ )
     { circle( copy, corners[i], r, Scalar(rng.uniform(0,255), rng.uniform(0,255),
              rng.uniform(0,255)), -1, 8, 0 ); }

  /// Show what you got
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  imshow( source_window, copy );
}
